import requests
import json
import pandas as pd
import numpy as np
import time



# 修改日期
# dm = world.loc[world.country == '丹麦', :]

def generate_date(df):
    """

    :param df: pd.DataFrame
    :return: pd.DataFrame
    """
    years = ['2022', '2021', '2020']
    pos = 0
    dates = []
    for date in df.date:
        date = str(date)
        if date == '12.31':
            pos += 1
        date = date.split('.')
        date.insert(0, years[pos])
        dates.append(''.join(date))
    return dates


# 获取citycode  以requests.get方式调用API接口，获取JSON格式的数据
# jiangsu:江苏省，根据需要进行修改
province = 'hubei'
data = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=" + province)  # jiangsu:江苏省
data = data.json()
citycode = pd.DataFrame(data['data']['city'])


# 获取地市数据  以requests.get方式调用API接口，获取JSON格式的数据
results = []
for code in citycode['citycode']:
    if code == '':
        continue
    print(code)
    time.sleep(np.random.randint(1, 5)) # 随机休眠1-5s
    # url为目标地址，要求 排序：按时间 搜索范围：标题 搜索关键词：湖北省新冠肺炎疫情情况
    data_ = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=city&citycode=" + code)
    data_ = data_.json()
    df = pd.DataFrame(data_['data']['historylist'])
    df['city'] = citycode.loc[citycode['citycode'] == code, 'name'].values[0]
    df['province'] = province
    results.append(df)

# 返回江苏省各地市疫情历史数据
jiansu = pd.concat(results)
jiansu.to_csv('jiansu.csv', encoding='gbk')


# 获取上海数据
data_2 = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=shanghai")
data_2 = data_2.json()
df = pd.DataFrame(data_2['data']['historylist'])
df['city'] = '上海'


# 获取中国数据
# 全部中国
file = open(r'D:\pycharm\temp\covid-19\txt')
data_ = file.readlines()
data_ = json.loads(data_[0])
df = pd.DataFrame(data_['data']['historylist'])
df['country'] = '中国'
df.to_csv('./covid-19/china.csv', encoding='gbk')


# 内地各省
data = requests.get(url="https://gwpre.sina.cn/interface/wap_api/feiyan/sinawap_get_area_tree.d.json")
data = data.json()
citycode = pd.DataFrame(data['data']['cities_cn'])
results = []
for name, code in zip(citycode['c'], citycode['e']):
    if code == '':
        continue
    print(code)
    time.sleep(np.random.randint(1, 5)) # 随机休眠1-5s
    # url为目标地址，要求 排序：按时间 搜索范围：标题 搜索关键词：湖北省新冠肺炎疫情情况
    data_ = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=" + code)
    data_ = data_.json()
    df = pd.DataFrame(data_['data']['historylist'])
    df['province'] = name
    df['dates'] = generate_date(df)
    results.append(df)

china = pd.concat(results)
china.to_csv('./covid-19/china.csv', encoding='gbk')


dd = pd.read_csv(r'C:\Users\doudou\PycharmProjects\temp\covid-19\china.csv', encoding='gbk', )
dd = dd.loc[~dd.province.isin(['香港', '澳门', '台湾']), :]

dd['ymd'] = pd.to_datetime(dd['ymd'])
cn = dd.groupby('ymd')['conNum', 'cureNum', 'deathNum'].sum()
cn.to_csv('./covid-19/cn.csv', encoding='gbk')


# 内地各市
data = requests.get(url="https://gwpre.sina.cn/interface/wap_api/feiyan/sinawap_get_area_tree.d.json")
data = data.json()
citycode = pd.DataFrame(data['data']['cities_cn'])

zhixiashi = []
results = []
for province, province_name, list_ in zip(citycode.e, citycode.c, citycode.z):
    if list_ == []:
        zhixiashi.append(province)
        continue
    # 获取citycode  以requests.get方式调用API接口，获取JSON格式的数据
    # jiangsu:江苏省，根据需要进行修改
    # province = 'jiangsu'
    data = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=" + province)  # jiangsu:江苏省
    data = data.json()
    citycode_ = pd.DataFrame(data['data']['city'])
    # 获取地市数据  以requests.get方式调用API接口，获取JSON格式的数据
    for code in citycode_['citycode']:
        if code == '':
            continue
        print(province_name, code)
        time.sleep(np.random.randint(1, 5)) # 随机休眠1-5s
        # url为目标地址，要求 排序：按时间 搜索范围：标题 搜索关键词：湖北省新冠肺炎疫情情况
        data_ = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=city&citycode=" + code)
        data_ = data_.json()
        df = pd.DataFrame(data_['data']['historylist'])
        df['city'] = citycode_.loc[citycode_['citycode'] == code, 'name'].values[0]
        df['province'] = province_name
        results.append(df)


# 获取直辖市数据
res = []
names = {'beijing': '北京', 'chongqing': '重庆', 'shanghai': '上海', 'tianjin':'天津', 'taiwan': '台湾', 'aomen': '澳门', 'xianggang': '香港'}
for code in zhixiashi:
    data_2 = requests.get(url="https://gwpre.sina.cn/interface/news/ncp/data.d.json?mod=province&province=" + code)
    data_2 = data_2.json()
    df = pd.DataFrame(data_2['data']['historylist'])
    df['city'] = names[code]
    df['province'] = names[code]
    res.append(df)

zxs = pd.concat(res)

res_2 = []
for city in set(zxs.city):
    df_temp = zxs.loc[zxs.city == city, :]
    df_temp['dates'] = generate_date(df_temp)
    res_2.append(df_temp)
zxs_ = pd.concat(res_2)
zxs_.to_csv('zxs.csv', encoding='gbk')


# 返回各地市疫情历史数据
Inland = pd.concat(results)

res_ = []
for city in set(Inland.city):
    df_temp = Inland.loc[Inland.city == city, :]
    df_temp['dates'] = generate_date(df_temp)
    res_.append(df_temp)
Inland_ = pd.concat(res_)
Inland_.to_csv('Inland.csv', encoding='gbk')








# 国家疫情
# 获取区域code  以requests.get方式调用API接口，获取JSON格式的数据
data = requests.get(url="https://gwpre.sina.cn/interface/wap_api/feiyan/sinawap_get_area_tree.d.json")
data = data.json()
countrycode = pd.DataFrame(data['data']['countries'])


results = []
for name, code in zip(countrycode['c'], countrycode['i']):
    if code == '':
        continue
    print(code)
    time.sleep(np.random.randint(1, 5)) # 随机休眠1-5s
    # url为目标地址，要求 排序：按时间 搜索范围：标题 搜索关键词：湖北省新冠肺炎疫情情况
    data_ = requests.get(url="https://gwpre.sina.cn/interface/news/wap/ncp_foreign.d.json?citycode=" + code)
    data_ = data_.json()
    df = pd.DataFrame(data_['data']['historylist'])
    df['country'] = name
    results.append(df)

world = pd.concat(results)
world.to_csv('./covid-19/world.csv', encoding='gbk')


# 修改日期
# dm = world.loc[world.country == '丹麦', :]

def generate_date(df):
    """

    :param df: pd.DataFrame
    :return: pd.DataFrame
    """
    years = ['2022', '2021', '2020']
    pos = 0
    dates = []
    for date in df.date:
        date = str(date)
        if date == '12.31':
            pos += 1
        date = date.split('.')
        date.insert(0, years[pos])
        dates.append(''.join(date))
    return dates


res = []
for coutry in set(world.country):
    df_temp = world.loc[world.country == coutry, :]
    df_temp['dates'] = generate_date(df_temp)
    res.append(df_temp)
worlds = pd.concat(res)
worlds.to_csv('./covid-19/worlds.csv', encoding='gbk')



# 无症状感染者
def cc(strs):
    ll = strs.split('.')
    tt = ['2022']
    for v in ll:
        if len(v) < 2:
            tt.append('0' + v)
        else:
            tt.append(v)
    return ''.join(tt)
# cc(strs)


results = []
urls = {'上海':'https://voice.baidu.com/newpneumonia/getv2?from=mola-virus&stage=publish&target=trend&isCaseIn=1&area=%E4%B8%8A%E6%B5%B7',
        '北京':'https://voice.baidu.com/newpneumonia/getv2?from=mola-virus&stage=publish&target=trend&isCaseIn=1&area=%E5%8C%97%E4%BA%AC',
        '长春':'https://voice.baidu.com/newpneumonia/getv2?from=mola-virus&stage=publish&target=trendCity&area=%E5%90%89%E6%9E%97-%E9%95%BF%E6%98%A5',
        '吉林':'https://voice.baidu.com/newpneumonia/getv2?from=mola-virus&stage=publish&target=trendCity&area=%E5%90%89%E6%9E%97-%E5%90%89%E6%9E%97%E5%B8%82',
        '哈尔滨':'https://voice.baidu.com/newpneumonia/getv2?from=mola-virus&stage=publish&target=trendCity&area=%E9%BB%91%E9%BE%99%E6%B1%9F-%E5%93%88%E5%B0%94%E6%BB%A8'
        }
for key, value in urls.items():
    data_2 = requests.get(url=value)
    data_2 = data_2.json()
    date = [cc(v) for v in data_2['data'][0]['trend']['updateDate']]
    if key in ['上海', '北京']:
        wzz = data_2['data'][0]['trend']['list'][5]['data']
    else:
        wzz = data_2['data'][0]['trend']['list'][0]['data']
    df = pd.DataFrame({'date':date, 'wzz':wzz})
    df['city'] = key
    results.append(df)

df_temp = pd.concat(results)
df_temp.sort_values(['city', 'date'], ascending=False, inplace=True)
df_temp.to_csv('./covid-19/wzz.csv', encoding='gbk')
